package com.summer.interceptors;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.summer.utils.MyUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
 * @Classname JWTinterceptos
 * &Description 拦截器
 *
 */
@Component
public class JWTinterceptos implements HandlerInterceptor {
    MyUtil myUtil = new MyUtil();
    Boolean flag;
    /*
     *
     *&Description 拦截验证token

     *Param [request, response, handler]
     *return boolean false lan
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        System.out.println("3333");
        Map<String, Object> map = new HashMap<>();
        flag=myUtil.judgeToken(token);
        System.out.println(flag);
            if(flag) {
                map.put("state",false);
                String json = new ObjectMapper().writeValueAsString(map);
                response.setContentType("applaction/json;charset=UTF-8");
                response.getWriter().println(json);
                return !flag;
            }
//            else
//            {
//                map.put("state",false);
//                System.out.println("2222");
//                //将map转换为json
//                String json = new ObjectMapper().writeValueAsString(map);
//                response.setContentType("applaction/json;charset=UTF-8");
//                response.getWriter().println(json);
//                return false;
//            }
        return false;
    }
}
